﻿using Quartz;
using Quartz.Impl;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace WeChat.Quartz
{
    public class CommissionExample:IExample
    {
        private static readonly CommissionExample keywordsTransExample = new CommissionExample();
        private CommissionExample()//不允许外部new
        {
        }
        /// <summary>
        /// 获取单例的SearchQueueOfNews
        /// </summary>
        /// <returns></returns>
        public static CommissionExample GetExample()
        {
            return keywordsTransExample;
        }

        public ISchedulerFactory SF
        {
            get { return new StdSchedulerFactory(); }
        }
        public IScheduler Sched { get { return SF.GetScheduler(); } }

        public string Name
        {
            get
            {
                throw new NotImplementedException();
            }
        }

        public void Run()
        {

            //计算距离下次运行需要的时间
            DateTimeOffset runTime = DateBuilder.EvenMinuteDate(DateTimeOffset.UtcNow);

            IJobDetail job = JobBuilder.Create<CommissionJob>()
                .WithIdentity("job1", "group1")
                .Build();

            ISimpleTrigger trigger = (ISimpleTrigger)TriggerBuilder.Create()
                                           .WithIdentity("trigger1", "group1")
                                           .StartAt(runTime)
                                           .WithSimpleSchedule(x => x.WithIntervalInMinutes(5).WithRepeatCount(999999))
                                           .Build();

            // Tell quartz to schedule the job using our trigger
            Sched.ScheduleJob(job, trigger);

            // Start up the scheduler (nothing can actually run until the 
            // scheduler has been started)
            Sched.Start();
        }

        public void Finish()
        {
            Sched.Shutdown(true);
        }
    }
}